//函数库

//封装选择器函数
function $(selector){
    doms = document.querySelectorAll(selector)
    if(doms.length ===1 ){
        return doms[0]
    }else{
        return doms
    }
}

//获取查询字符
function getQuaryString(key){
    var str = location.search.substring(1)
    var arr1 = str.split("&")
    var result = null
    arr1.forEach((item)=>{
        var arr2 = item.split("=")
        if(arr2[0] === key){
            result = arr2[1]
        }
    })
    return result
}


//封装offset函数
function offset(dom){
    var ofl = 0 - dom.clientLeft
    var oft = 0 - dom.clientTop
    while(dom){
        ofl += dom.clientLeft + dom.offsetLeft
        oft += dom.clientTop + dom.offsetTop
        dom = dom.offsetParent
    }
    return {left:ofl,top:oft}
}

//封装淡入淡出函数
// function(){

// }

//animate函数
function animate(ele, obj, fn) {
    clearInterval(ele.timeId);
    ele.timeId = setInterval(function () {
        var flag = true;
        for (var k in obj) {
            if (k === "opacity") {
                var leader = parseInt((getStyle(ele, k) || 0) * 100);
                var target = obj[k] * 100
                var step = (target - leader) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                leader = leader + step;
                ele.style[k] = leader / 100;
            } else if (k === "zIndex") {
                var leader = parseInt(getStyle(ele, k) || 0);
                var target = obj[k];
                ele.style[k] = target;
            } else {
                var leader = parseInt(getStyle(ele, k) || 0);
                var target = obj[k];
                var step = (target - leader) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                leader = leader + step;
                ele.style[k] = leader + "px";
            }
            if (leader !== target) {
                flag = false;
            }
        }
        if (flag) {
            clearInterval(ele.timeId);
            if (fn && typeof fn === "function") {
                fn();
            }
        }
    }, 30)
}
//封装兼容性
function getStyle(ele, attr) {
    if (ele && ele.currentStyle) {
        return ele.currentStyle[attr];
    } else {
        return window.getComputedStyle(ele, null)[attr];
    }
}
